Export to Different File Format

From Documentation



Export to Different File Format



Stop.png This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.

Purpose

Export ZK Spreadsheet to different file format (PDF, Html, CSV, Tab delimited and so on)

Exporter interface

Exporting ZK Spreadsheet to another format is done through Exporter interface. It has three convenient methods to export ZK Spreadsheet contents.

  • Use Exporter#export(Book workbook, OutputStream outputStream) API to export entire workbook.
  • Use Exporter#export(Worksheet worksheet, OutputStream outputStream); API to export specific worksheet.
  • Use Exporter#exportSelection(Worksheet worksheet, AreaReference area, OutputStream outputStream) API to export partially selected area of workbook.

You can use Exporters to get specific Exporter implementation. For example to get Exporter implementation for PDF format use

Exporter pdfExporter = Exporters.getExporter("pdf");

You can also implement Exporter for exporting to another format such as CSV or HTML. You will need to register your implementation in zk.xml by defining org.zkoss.zss.model.Exporter.class library property. For example

	<library-property>
		<name>org.zkoss.zss.model.Exporter.class</name>
		<value>html=org.xyz.com.HtmlExporter</value>
	</library-property>

NOTE: Exporter type name and implementing class name is separated with = character. You can register multiple Exporter implementations by separating type-name=class-name pairs by a comma.

ZUML

Here is an example of ZUML file that displays an Excel book file and has three buttons to export entire workbook,selected sheet and specific selection.

<?page title="ZSS Export to Different File Format" contentType="text/html;charset=UTF-8"?>
<window width="100" height="100%"
	apply="org.zkoss.zssessentials.export.ExportComposer">
	<hbox>
		<button id="exportBtn" label="Export All"></button>
	</hbox>
	<spreadsheet id="spreadsheet"
		src="/WEB-INF/excel/export/export.xlsx" maxrows="200" maxcolumns="40"
		vflex="1" width="100%">
	</spreadsheet>
</window>

Composer

In composer there is one event handler each for export button defined in the above ZUML. To export the entire ZK Spreadsheet workbook content use Exporter#export(Book,OutputStream) API. You should be able to get Book instance either by importing Excel book file using Importer interface or using Spreadsheet.getBook() API if ZK Spreadsheet component is already initialized with an Excel book file. You should also prepare OutputStream to which new format should be written to. For example

	public void onClick$exportBtn(Event evt) throws IOException {
		Book wb = spreadsheet.getBook();
		Exporter c = Exporters.getExporter("pdf");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		c.export(wb, baos);
		Filedownload.save(baos.toByteArray(), "application/pdf",
				wb.getBookName());
	}

Here we use Exporters to get Exporter implementation to export ZK Spreadsheet contents to PDF format. Similarly you could implement Exporter to export to CSV,HTML etc. formats.

See the full source code for Composer here

Version History

Last Update : 2022/01/19


Version Date Content
     


All source code listed in this book is at Github.


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.